Capture Credit Card Information

In this guide, we will guide you on how to Capture Credit-Card information and retrieve the payment method token using ApteanPay js SDK.

To Capture Credit-Card information, perform the following steps.

  1. Create a html file.

    <html>
      <head></head>
      <body>
      </body>
    </html>
  2. Add following code to html body.

    <div id="creditCardIframe"></div>
  3. Add following code to html body.

    <button id="submitButton"> click Me </button>
  4. Add following code to html body and replace Aptean js SDK url here with JS SDK url

    <script src="<<<Aptean js SDK url here>>>"> </script>
  5. Add following code to html body.

    <script>
      // initialize Aptean Pay
      const apteanPay = ApteanPay(
        '<<<apim key here>>>', // api key
        '<<<product id here>>>', // erp product id
        '<<<tenant id here>>>' // tenant id
      );
    
      // style an iframe
      // please see the "Style iFrame" document for more information on styling
      var customStyle = {
        styles: {
          base: {
            'border-radius': '4px',
            height: '56px',
            'font-size': '16px',
          }
        },
      };
      
      var cardComponentOptions = {
        customStyle,
        showPlaceholders: true,
        showErrorMessages: true
      };
    
      // create builder objects
      const components = apteanPay.components({});
      const cardComponent = components.create('card', cardComponentOptions);
    
      // mount the form to the DOM
      cardComponent.mount(
        'creditCardIframe', // div Id
        '#submitButton' // button Id
      );
    
      // this method returns a callback function.
      apteanPay.createTokenCallback(
        cardComponent,
        {
          name: '<<<customer name here>>>',  //mandatory to pass
          addressLine1: '<<<customer addressLine1 here>>>',
          addressLine2: '<<<customer addressLine2 here>>>',
          addressCity: '<<<customer addressCity here>>>',
          addressState: '<<<customer addressState here>>>',
          addressZip: '<<<customer addressZip here>>>', //mandatory to pass
          addressCountry: '<<<customer addressCountry here>>>',  //mandatory to pass
          emailAddress: '<<<customer emailAddress here>>>', //mandatory to pass
          phoneCountryCode: '<<<customer phoneCountryCode here>>>', 
          phoneNumber: '<<<customer phoneNumber here>>>',
        },
        function(token, error) {
          // error object
          if (error) {
            console.log(error);
          }
    
          // token object
          if (token) {
            console.log(token);
          }
        }
      );
    </script>
  6. Fill your respective values appropriately.
  7. Save the file and open the html file in browser. You will get to see html page as below.
    credit_card_capture
  8. Provide one of the following test credit-Card information and click the button. Use any CVV/CVV2 number and expiration month year valid for the current year:

    Card Type Card Number
    Visa 4111111111111111
    MasterCard 5431111111111111
    Discover 6011000991300009
    American Express 341111111111111
    Diner's Club 30205252489926
    JCB 3541963594572595
    Maestro 6799990100000000019
  9. You can get the token id from the console as id field in json. These tokens are valid for only 15 minutes.

    Success Token Response sample:

    {id: "xxxxxxxx-0314-4a64-a974-9828c4a5c28d", created: 1612267767, livemode: false, type: "credit-card", used: false, …}

    Failure Token Response sample:

    {type: "validation_error", message: "Parameter failed UI validation.", param: "expiration-year"}
  10. Once you have a valid token it can be saved as a long term token to be used for future payments if that is the desired use case. Call the following method - please refer the GraphQL Playground https://stg.api.apteanpay.com/ for full documentation:

    mutation { createPaymentMethod(input: {attachToResourceId:"<<<tenant ID>>>",
      isDefault:false,
      token:"<<<token ID>>>"})
      {
        code
        error
        message
        paymentMethod {id}
      }
    }
  11. You can use the following methods to simulate other responses for testing:

NOTE: In case you get error response in console, refer to the following possible error scenarios. In production environment there could be other errors returned based on invalid cards, denied by bank, etc.
Error Type Error Message Error Reason
validation_error Parameter failed UI validation :expiration-month Provided expiration month and year combination is not valid
validation_error Parameter failed UI validation : expiration-year Provided expiration month and year combination is not valid
validation_error Parameter failed UI validation : cvv-number Provided cvv-number is not valid
validation_error Expected value in [PR, PS, PT, PW, PY, QA, AD, AE,… NU, NZ, OM, PA, PE, PF, PG, PH, PK, PL, PM, PN]. Provided addressCountry is not valid
validation_error Invalid postal code for the country '{addressCountry}'. Provided addressZip is not valid
validation_error Expected a value matching the regular expression '^[0-9]{7,14}$'. Invalid phone number
api_error Expected a value matching the regular expression '^([^,:;=@\"'\\\\\\s()\\[\\]]+)+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$' Invalid email